<template>
  <el-row>
    <el-col :span="8" :offset="8">
      <h2>用户注册</h2>
      <el-form label-width="130px">
        <el-form-item label="用户名:">
          <el-input v-model="form.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="form.password" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="确认密码:">
          <el-input v-model="form.confirmPassword" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="公司名称:">
          <el-input v-model="form.companyName" clearable></el-input>
        </el-form-item>
        <el-form-item label="公司区块链地址：">
          <el-input v-model="form.companyAddress"></el-input>
        </el-form-item>
        <el-form-item label="公司类型:">
          <el-select v-model="form.companyType" placeholder="请选择:">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司实际地址" v-if="form.companyType == 2">
          <el-input v-model="form.location"></el-input>
        </el-form-item>
        <el-form-item label="公司经营范围" v-if="form.companyType == 2">
          <el-input v-model="form.bussinessScope"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onRegister" style="margin-right: 60px">注册</el-button>
          <el-button type="primary" @click="goback" style="margin-right: 60px">返回</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      form: {
        username: '',
        password: '',
        confirmPassword: '',
        companyAddress: '',
        companyName: '',
        companyType: '',
        location: '',//公司实际地址
        bussinessScope: '' //公司经营范围
      },
      options: [{
        value: '0',
        label: '管理员'
      }, {
        value: '1',
        label: '采购公司'
      }, {
        value: '2',
        label: '运输公司'
      }]
    }
  },
  methods: {
    onRegister() {
      if (this.form.password != this.form.confirmPassword) {
        alert("密码不匹配")
      } else {
        this.axios.post('/register', this.form).then((res) => {
          let code = res.data.code
          if (code == 200) {
            alert("注册成功")
            this.$router.push('/Login')
          } else {
            alert(`注册失败，原因为${res.data.data}`)
          }
        })
          .catch((res) => {
            console.log(res);
          })
      }

    },
    goback() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped></style>